package com.xuzimian.globaldemo.office.excel.pojo;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.base.Splitter;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Lists;
import com.google.common.collect.Table;

/**
 * 用于记录Excel上有占位符的单元格地址。
 * 
 * @author xuzimian
 */
public class ExcelReplaceAddress {

    public ExcelReplaceAddress() {
    }

    private Table<String, String, ExcelAddress> keysMap = HashBasedTable.create();

    public void saveAddress(int col, int row, String key) {
        ExcelAddress address = new ExcelAddress();
        address.setColumn(col);
        address.setRow(row);
        List<String> keys = this.splitKey(key);
        int keysSize = keys.size();
        keysMap.put(keysSize > 0 ? keys.get(0) : key, keysSize > 1 ? keys.get(1) : "null", address);
    }

    public Map<String, ExcelAddress> getAddress(String firstKey) {
        return keysMap.row(firstKey);
    }

    public ExcelAddress getAddress(String firstKey, String secondKey) {
        return keysMap.get(firstKey, secondKey);
    }

    public Table<String, String, ExcelAddress> getKeyMaps() {
        return this.keysMap;
    }

    private List<String> splitKey(String key) {
        Splitter splitter = Splitter.on(".");
        return Optional.ofNullable(key).map(d -> splitter.splitToList(d)).orElseGet(() -> Lists.newArrayList());
    }
}